package com.dongdongshop.aop;


import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.json.JSONUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


//添加注入spring容器的注解
@Component
@Aspect//加上此注解的类就表示一个切面类
public class LogApp {

    private Logger logger = LoggerFactory.getLogger(LogApp.class);
    //表示这个路径下的所有方法,annotation注解里的内容是自己自定义的路径,表示这个注解也要走这个路径
    @Pointcut("execution(public * com.dongdongshop.service.*.*(..)) || @annotation(com.dongdongshop.log.DKlog)")
    public void pointcut(){

    }
    
    //前置通知
    @Before("pointcut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable{
        Object[] args = joinPoint.getArgs();//被调用的方法的参数
        Signature signature = joinPoint.getSignature();//被调用的方法的名字
        logger.info("方法 {} 正在被调用,参数: {} ",signature,args);
    }

    //返回后通知
    @AfterReturning(value = "pointcut()",returning = "result")
    public void doAfter(Object result) throws Throwable{
        logger.info("方法调用成功,返回值为: {} ",JSONUtil.toJsonStr(result));
    }

}
